The Scripting app allows you to query quantity-based health data, such as step count, heart rate, body mass, calories burned, distance, and more, using the global Health.queryQuantitySamples()
API.
This guide explains how to retrieve quantity samples and work with the results.
A quantity sample represents a numeric health measurement taken at a specific time or over a time interval. Common examples include:
stepCount
heartRate
bodyMass
activeEnergyBurned
distanceWalkingRunning
These samples can be either discrete (a single measurement) or cumulative (a value summed over time).
quantityType
: The health data type to query (e.g., "stepCount"
, "heartRate"
)startDate
/ endDate
: Time range for filtering sampleslimit
: Maximum number of resultsstrictStartDate
/ strictEndDate
: If true
, only samples starting/ending exactly at those dates will be includedsortDescriptors
: Optional array to sort results by startDate
, endDate
, or count
Each sample returned may be:
HealthQuantitySample
: The base classHealthCumulativeQuantitySample
: Includes .sumQuantity(unit)
HealthDiscreteQuantitySample
: Includes .averageQuantity(unit)
, .maximumQuantity(unit)
, etc.You can use instanceof
or feature detection to check for extended properties.
Example:
.quantityValue()
must match the type (e.g., use count()
for steps, gram(HealthMetricPrefix.kilo)
for body mass).count().divided(minute())
.startDate
and endDate
on each sample..metadata
and .count
.Quantity Type | Recommended Unit |
---|---|
"stepCount" |
HealthUnit.count() |
"heartRate" |
HealthUnit.count().divided(HealthUnit.minute()) |
"bodyMass" |
HealthUnit.gram(HealthMetricPrefix.kilo) |
"activeEnergyBurned" |
HealthUnit.kilocalorie() |
"distanceWalkingRunning" |
HealthUnit.meter() |
To read quantity samples:
Health.queryQuantitySamples(type, options)
.quantityValue(unit)
or .sumQuantity(unit)
depending on the typeThis API gives you powerful access to time-series health data stored in HealthKit.